//
//  LLVerticalScrollADView.m
//  ProtocolTest
//
//  Created by admin on 2020/1/7.
//  Copyright © 2020 LG. All rights reserved.
//

#import "LLVerticalScrollADView.h"

@interface LLVerticalScrollADView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView<LLVerticalScrollADViewDataSource> *firstView;
@property (nonatomic, strong) UIView<LLVerticalScrollADViewDataSource> *middleView;
@property (nonatomic, strong) UIView<LLVerticalScrollADViewDataSource> *lastView;

@property (nonatomic, strong) NSMutableArray *datasArray;

@property (nonatomic, assign) id<LLVerticalScrollADViewDelegate> delegate;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) NSInteger count;

@end

@implementation LLVerticalScrollADView

- (instancetype)initWithFrame:(CGRect)frame datasArray:(NSArray *)datasArray delegate:(nonnull id<LLVerticalScrollADViewDelegate>)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = delegate;
        [self setupDatasArray:datasArray];
    }
    return self;
}

- (void)setupDatasArray:(NSArray *)datasArray
{
    if (datasArray.count == 0) {
        return;
    }
    
    [self.datasArray removeAllObjects];
    
    [self.datasArray addObject:datasArray.lastObject];
    [self.datasArray addObjectsFromArray:datasArray];
    [self.datasArray addObject:datasArray.firstObject];
    
    [self setupSubviews];
    
    self.count = 1;
    
    [self setupContent];
    
    if (datasArray.count > 1) {
        [self removeTimer];
        [self addTimer];
    } else {
        [self removeTimer];
    }
    
    
}

- (void)addTimer
{
    YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:proxy selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

///计时器方法
- (void)timerAction
{
    self.count++;
    if (self.count >= self.datasArray.count - 1) {
        self.count = 1;
    }
    
    // 设置偏移量
    [self.scrollView setContentOffset:CGPointMake(0, self.frame.size.height * 2) animated:YES];
    
}


//计时器自动滚动结束才调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self setupContent];
    [self.scrollView setContentOffset:CGPointMake(0, self.frame.size.height) animated:NO];
}


- (void)setupContent
{
    [self.firstView setupData:self.datasArray[self.count - 1]];
    [self.middleView setupData:self.datasArray[self.count]];
    [self.lastView setupData:self.datasArray[self.count + 1]];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    self.firstView.frame = self.bounds;
    self.middleView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height);
    self.lastView.frame = CGRectMake(0, self.frame.size.height * 2, self.frame.size.width, self.frame.size.height);
    
    [self.firstView setNeedsLayout];
    [self.middleView setNeedsLayout];
    [self.lastView setNeedsLayout];
    
}

- (void)setupSubviews
{
    if ([self.subviews containsObject:self.scrollView]) {
        return;
    }
    
    [self.scrollView addSubview:self.firstView];
    [self.scrollView addSubview:self.middleView];
    [self.scrollView addSubview:self.lastView];
    
    [self addSubview:self.scrollView];
    
    // 设置偏移量
    [self.scrollView setContentOffset:CGPointMake(0, self.frame.size.height)];
    
}

- (UIView<LLVerticalScrollADViewDataSource> *)firstView
{
    if (!_firstView) {
        _firstView = [self.delegate createADViewWithVerticalScrollADView:self];
    }
    return _firstView;
}

- (UIView<LLVerticalScrollADViewDataSource> *)middleView
{
    if (!_middleView) {
        _middleView = [self.delegate createADViewWithVerticalScrollADView:self];
    }
    return _middleView;
}

- (UIView<LLVerticalScrollADViewDataSource> *)lastView
{
    if (!_lastView) {
        _lastView = [self.delegate createADViewWithVerticalScrollADView:self];
    }
    return _lastView;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height * 3);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.scrollEnabled = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    }
    return _scrollView;
}

- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
    }
    return _datasArray;
}


@end
